package com.example.demo.controller;

import com.example.demo.dao.Blog_img;
import com.example.demo.service.impl.Blog_imgserviceimpl;
import com.example.demo.utils.CommonRetumType;
import com.example.demo.utils.error.BusinessException;
import com.example.demo.utils.error.EmBusinessError;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/blogimg")
public class BlogImgController extends BaseController{

    @Autowired
    Blog_imgserviceimpl blog_imgserviceimpl;

    @GetMapping("/read{id}")
    public CommonRetumType findid(@RequestParam("id") Integer id)
    {
        Blog_img blog_img = blog_imgserviceimpl.selectByPrimaryKey(id);
        return CommonRetumType.create(blog_img);
    }

    @GetMapping("/insert")
    public CommonRetumType insert(Blog_img blog_img) throws BusinessException {
        Integer blog_img1 = blog_imgserviceimpl.insert(blog_img);
        if (blog_img1 != null)
        {
            return CommonRetumType.create("插入成功");
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_INSERT_ERROR);
        }
    }

    @PutMapping("/update")
    public CommonRetumType update(Blog_img blog_img) throws BusinessException {
        Integer blog_img2 = blog_imgserviceimpl.updateByPrimaryKey(blog_img);
        if (blog_img2 != null)
        {
            return CommonRetumType.create("更新成功");
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_UPDATE_ERROR);
        }
    }

    @DeleteMapping("/delete{id}")
    public CommonRetumType delete(@RequestParam("id") Integer id) throws BusinessException {
        Integer blog_img3 = blog_imgserviceimpl.deleteByPrimaryKey(id);
        if (blog_img3 != null)
        {
            return CommonRetumType.create("删除成功");
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_DELETE_ERROR);
        }
    }

    @GetMapping("/index")
    public CommonRetumType index() throws BusinessException {
        List<Blog_img> blog_img = blog_imgserviceimpl.index();
        if (blog_img != null){
            return CommonRetumType.create(blog_img);
        }else {
            throw new BusinessException(EmBusinessError.PARAMETER_INDEX_ERROR);
        }

    }


}
